<!--
 * @Author: cgl
 * @Date: 2023-04-18 15:12:26
 * @LastEditors: cgl
 * @LastEditTime: 2023-09-07 14:38:37
 * @FilePath: \public-health-emergencies-fed:\新放射\新radiation-hygiene-fe\src\views\person\entrustEquipment.vue
 * @Description:
 *
 * Copyright (c) 2023 by cgl/微盛信息产业有限公司, All Rights Reserved.
-->
<template>
  <div class="app-container">
    <el-form
      class="searchform"
      :model="queryParams"
      ref="queryParams"
      size="small"
      inline
    >
      <el-form-item label="单位名称" label-width="80px">
        <m-select
          v-model="queryParams.entrustOrganizationId"
          placeholder="输入文字检索查询"
          style="width: 250px"
          filterable
          clearable
          :lists="entrustOrganizationSelect"
          lists_label="organizationName"
          lists_value="entrustOrganizationId"
          lists_key="entrustOrganizationId"
          :total="orgTotal"
          :pageSize="1000"
          @handleChange="listeningEvent"
        >
        </m-select>
      </el-form-item>
      <el-form-item label="购买时间" label-width="80px">
        <el-date-picker
          v-model="queryParams.formDate"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="['00:00:00', '23:59:59']"
          clearable
          style="width: 250px"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="设备名称" label-width="80px">
        <el-select
          style="width: 250px"
          v-model="queryParams.equipmentId"
          placeholder="输入文字检索查询"
          filterable
          clearable
        >
          <el-option
            :label="item.equipmentName"
            :value="item.equipmentId"
            v-for="(item, index) in entrustEquipmentSelect"
            :key="index"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="设备编号" label-width="80px">
        <el-input
          style="width: 150px"
          v-model="queryParams.equipmentNumber"
          placeholder="请输入"
          clearable
          @keyup.enter.native="getList()"
        />
      </el-form-item>
      <el-form-item label-width="20px">
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="getList()"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="reset()"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <div class="table-wrapper">
      <div class="tableoperation">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click.native.prevent="openDialog()"
          >添加</el-button
        >
        <el-button
          type="primary"
          icon="el-icon-upload"
          size="mini"
          plain
          @click.native.prevent="handleOpenImportDialog()"
          >导入</el-button
        >
        <el-button
          type="primary"
          icon="el-icon-download"
          size="mini"
          plain
          @click.native.prevent="handleExport()"
          >导出</el-button
        >
        <el-button
          type="primary"
          icon="el-icon-upload"
          size="mini"
          plain
          @click.native.prevent="handleUploadPointImg()"
          >上传点位图</el-button
        >
      </div>

      <el-table
        v-loading="loading"
        :data="entrustEquipmentList"
        @row-dblclick="(row) => openDialog(row, '3')"
      >
        <el-table-column label="序号" type="index" align="center" width="50">
          <template slot-scope="scope">
            <div style="text-align: center;">{{ scope.$index + 1 }}</div>
          </template>
        </el-table-column>
        <el-table-column
          label="单位名称"
          align="center"
          prop="entrustOrganizationName"
          :show-overflow-tooltip="true"
        />
        <el-table-column label="设备名称" align="center" prop="equipmentName" />
        <el-table-column
          label="设备编号"
          align="center"
          prop="equipmentNumber"
        />
        <el-table-column
          label="规格及型号"
          align="center"
          prop="specificationModel"
        />
        <el-table-column
          label="生产厂家"
          align="center"
          prop="manufacturerName"
        />
        <el-table-column label="使用场所" align="center" prop="radiationSite" />
        <el-table-column label="购买时间" align="center" prop="purchaseTime" />
        <el-table-column label="设备状况" align="center">
          <template slot-scope="scope">
            <dict-tag
              :options="dict.type.equipment_state"
              :value="scope.row.state"
            />
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作" width="100">
          <template slot-scope="{ row }">
            <el-button @click.native.prevent="openDialog(row)" type="text">
              编辑
            </el-button>
            <el-button
              @click.native.prevent="handleDelete(row)"
              type="text"
              style="color: #f32d2d"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>

    <!-- 弹框 -->
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      width="1200px"
      v-dialogDrag="true"
    >
      <el-form
        :model="form"
        :disabled="status == '3'"
        :rules="rules"
        ref="form"
        size="small"
        label-width="100px"
      >
        <el-row>
          <el-col :span="8">
            <el-form-item label="委托单位" prop="entrustOrganizationId">
              <m-select
                v-model="form.entrustOrganizationId"
                placeholder="请选择"
                filterable
                style="width: 100%"
                clearable
                :lists="entrustOrganizationSelect"
                lists_label="organizationName"
                lists_value="entrustOrganizationId"
                lists_key="entrustOrganizationId"
                :total="orgTotal"
                :pageSize="1000"
                @handleChange="listeningEvent"
              >
              </m-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="设备名称" prop="equipmentName">
              <el-input
                v-model="form.equipmentName"
                placeholder="请输入"
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="设备类型" prop="entrustEquipmentTypeCode">
              <el-select
                v-model="form.entrustEquipmentTypeCode"
                @change="changeEquipmentType"
                placeholder="请选择"
                style="width: 100%"
                filterable
                clearable
              >
                <el-option
                  v-for="item in entrustEquipmentTypeSelect"
                  :key="item.equipmentTypeCode"
                  :label="item.name"
                  :value="item.equipmentTypeCode"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="放置场所" prop="radiationSite">
              <el-input
                v-model="form.radiationSite"
                placeholder="请输入"
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="设备编号">
              <el-input
                v-model="form.equipmentNumber"
                :placeholder="status != '3' ? '请输入' : ''"
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="规格及型号">
              <el-input
                v-model="form.specificationModel"
                :placeholder="status != '3' ? '请输入' : ''"
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="购买时间">
              <el-date-picker
                v-model="form.purchaseTime"
                value-format="yyyy-MM-dd"
                type="date"
                style="width: 100%"
                :placeholder="status != '3' ? '请选择' : ''"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="设备状况">
              <el-select
                v-model="form.state"
                :placeholder="status != '3' ? '请选择' : ''"
                style="width: 100%"
                clearable
              >
                <el-option
                  v-for="item in dict.type.equipment_state"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="生产厂家">
              <el-select
                v-model="form.manufacturer"
                :placeholder="status != '3' ? '请选择' : ''"
                filterable
                style="width: 100%"
                clearable
              >
                <el-option
                  v-for="item in manufactureSelectList"
                  :key="item.manufactureId"
                  :label="item.manufactureName"
                  :value="item.manufactureId"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item
              label="是否为车载设备"
              prop="isVehicleEquipment"
              class="inline"
            >
              <el-select
                v-model="form.isVehicleEquipment"
                :placeholder="status != '3' ? '请选择' : ''"
                style="width: 100%"
              >
                <el-option
                  v-for="item in dict.type.is_or_no"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="车牌号">
              <el-input
                v-model="form.carNum"
                :placeholder="status != '3' ? '请输入' : ''"
                :disabled="form.isVehicleEquipment == '0'"
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="是否CBCT">
              <el-select
                v-model="form.isCbct"
                placeholder="请选择"
                style="width: 100%"
              >
                <el-option
                  v-for="item in dict.type.is_or_no"
                  :key="item.value"
                  :label="item.label"
                  :value="Number(item.value)"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <div class="innertitle">基本情况</div>
      <div class="table" :class="{ mask: status == '3' }">
        <basicTable :basicForm="form" :lrstatus="status"></basicTable>
      </div>

      <div class="innertitle">点位图</div>
      <div style="margin: 12px 0; display: flex; justify-content: flex-end">
        <el-button
          icon="el-icon-refresh"
          size="mini"
          @click="handleOpenDragCanvas()"
          >设计平面图</el-button
        >
      </div>
      <div style="display: flex; justify-content: center">
        <div>
          <imageUpload
            :limit="1"
            :isShowTip="false"
            v-model="form.recordOssId"
          />
          <div style="text-align: center">原始记录点位图</div>
        </div>
        <div style="margin-left: 50px">
          <imageUpload
            :limit="1"
            :isShowTip="false"
            v-model="form.schematicImage"
          />
          <div style="text-align: center">报告点位图</div>
        </div>
      </div>

      <div slot="footer">
        <el-button type="primary" v-if="status != '3'" @click="submitForm"
          >确 定</el-button
        >
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 用户导入对话框 -->
    <el-dialog
      :title="upload.title"
      :visible.sync="upload.open"
      width="400px"
      append-to-body
      v-dialogDrag="true"
    >
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx"
        :headers="upload.headers"
        :action="upload.url"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <span>仅允许导入xlsx格式文件。</span>
          <el-link
            type="primary"
            :underline="false"
            style="font-size: 12px; vertical-align: baseline"
            @click="handleDownloadImportTemplate"
            >下载模板</el-link
          >
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>

    <UploadPointImageDialog
      :show.sync="showUploadPointImageDialog"
    ></UploadPointImageDialog>

    <DragCanvas ref="dragCanvasRefs" @confirm="handleConfirmDrag" />
  </div>
</template>

<script>
import {
  entrustEquipmentList,
  entrustEquipmentExport,
  entrustEquipmentDetail,
  entrustEquipmentAdd,
  entrustEquipmentEdit,
  entrustEquipmentDelete,
} from "@/api/person/entrustEquipment";
import { entrustEquipmentListing } from "@/api/person/entrustEquipment";
import {
  entrustOrganizationListing,
  entrustOrganizationList,
} from "@/api/person/entrustOrganization";
import { entrustEquipmentTypeListing } from "@/api/person/entrustEquipmentType";
import { manufactureListing } from "@/api/basicData/manufacture";
import addOrganization from "./addOrganization.vue";
import BasicTable from "@/views/slide/components/basicIndex";
import MSelect from "@/components/mSelect";
import DragCanvas from "@/views/slide/components/dragCanvas.vue";
import { getToken } from "@/utils/auth";
import UploadPointImageDialog from "./component/UploadPointImageDialog.vue";

export default {
  name: "EntrustEquipment",
  dicts: ["equipment_state", "is_or_no"],
  components: {
    MSelect,
    addOrganization,
    BasicTable,
    DragCanvas,
    UploadPointImageDialog,
  },
  data() {
    return {
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      total: 0,
      dialogVisible: false,
      rules: {
        entrustOrganizationId: [
          { required: true, message: "委托单位不能为空", trigger: "blur" },
        ],
        radiationSite: [
          { required: true, message: "放置场所不能为空", trigger: "blur" },
        ],
        equipmentName: [
          { required: true, message: "设备名称不能为空", trigger: "blur" },
        ],
        entrustEquipmentTypeCode: [
          { required: true, message: "设备类型不能为空", trigger: "blur" },
        ],
        isVehicleEquipment: [
          {
            required: true,
            message: "是否为车载设备不能为空",
            trigger: "blur",
          },
        ],
      },
      // 视图高度
      height: document.documentElement.clientHeight,
      // 委托单位设备列表
      entrustEquipmentList: [],
      // 表单参数
      form: {},
      loading: false,
      status: null,
      title: null,
      // 委托单位下拉框
      entrustOrganizationSelect: [],
      // 放置场所下拉框
      radiationSiteSelect: [],
      // 设备名称下拉框
      entrustEquipmentSelect: [],
      // 委托单位设备类型下拉框
      entrustEquipmentTypeSelect: [],
      // 委托单位设备类型下拉框
      manufactureSelectList: [],
      basicInfo: {},
      // 单位查询数据
      organizationQueryData: {
        organizationName: "", //单位名称模糊查询
        pageNum: 1, //页码
        pageSize: 10, //每页展示的条数
      },
      orgTotal: 0,

      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url:
          process.env.VUE_APP_BASE_API + "/basic/entrust/equipment/importData",
      },

      showUploadPointImageDialog: false,
    };
  },
  created() {
    this.getList();
    this.getEntrustEquipmentListing();
    this.getEntrustEquipmentTypeListing();
    this.getManufactureListing();
  },
  methods: {
    // 获取设备列表
    getList() {
      this.queryParams.purchaseTimeStart =
        this.queryParams.formDate && this.queryParams.formDate[0];
      this.queryParams.purchaseTimeEnd =
        this.queryParams.formDate && this.queryParams.formDate[1];
      let queryParams = { ...this.queryParams };
      delete queryParams.formDate;
      this.loading = true;
      entrustEquipmentList(queryParams)
        .then((response) => {
          this.entrustEquipmentList = response.rows;
          this.total = response.total;
          this.loading = false;
        })
        .catch((response) => {
          this.loading = false;
        });
    },
    // 获取单位下拉框
    listeningEvent(e) {
      let { queryName, pageNum, pageSize, type } = e;
      if (type == "scroll" && pageNum == this.organizationQueryData.pageNum)
        return;
      this.organizationQueryData.organizationName = queryName;
      this.organizationQueryData.pageNum = pageNum;
      this.organizationQueryData.pageSize = pageSize;
      entrustOrganizationList(this.organizationQueryData).then((response) => {
        if (type == "scroll") {
          this.entrustOrganizationSelect.push(...response.rows);
        } else {
          this.entrustOrganizationSelect = response.rows;
        }
        this.orgTotal = response.total;
      });
    },
    // 获取设备名称下拉框
    getEntrustEquipmentListing() {
      entrustEquipmentListing().then((response) => {
        this.entrustEquipmentSelect = response.data;
      });
    },
    // 获取设备类型下拉框
    getEntrustEquipmentTypeListing() {
      entrustEquipmentTypeListing().then((response) => {
        this.entrustEquipmentTypeSelect = response.data;
      });
    },
    // 获取生产厂家下拉框
    getManufactureListing() {
      manufactureListing().then((response) => {
        this.manufactureSelectList = response.data;
      });
    },
    // 更改设备类型
    changeEquipmentType(val) {
      this.basicInfo = {};
      let findobj = this.entrustEquipmentTypeSelect.find(
        (item) => item.equipmentTypeCode == val
      );
      if (findobj) {
        this.form.basicInfoCode = findobj.basicInfoCode;
        console.log("nnfasd", findobj.basicInfoCode);
      }
      this.form = { ...this.form, basicInfo: {} };
    },
    // 打开对话框
    openDialog(row, status) {
      this.resetForm("form");
      this.form.basicInfo = {};
      if (row) {
        entrustEquipmentDetail(row.equipmentId).then((response) => {
          this.form = response.data;
          if (response.data.state != undefined) {
            this.$set(this.form, "state", response.data.state.toString());
          }
          if (response.data.isVehicleEquipment != undefined) {
            this.$set(
              this.form,
              "isVehicleEquipment",
              response.data.isVehicleEquipment.toString()
            );
          }
          this.form.basicInfo = JSON.parse(this.form.basicInfo) || {};
          this.dialogVisible = true;
        });
        if (status == "3") {
          /* 查看 */
          this.title = "查看设备";
          this.status = "3";
        } else {
          this.title = "编辑设备";
          this.status = "2";
        }
      } else {
        this.form = {
          isVehicleEquipment: "0",
          basicInfo: {},
        };
        this.title = "添加设备";
        this.status = "1";
        this.dialogVisible = true;
      }
    },
    /** 导入按钮操作 */
    handleOpenImportDialog() {
      this.upload.title = "导入受检设备";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    handleDownloadImportTemplate() {
      this.download(
        "/basic/entrust/equipment/downloadTemplate",
        {},
        `受检设备导入模板.xlsx`
      );
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(
        "<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" +
          response.msg +
          "</div>",
        "导入结果",
        { dangerouslyUseHTMLString: true }
      );
      if (response.code == 200) this.getList();
    },
    handleOpenDragCanvas() {
      this.$refs.dragCanvasRefs.openDialog({
        imageJson: this.form.imageJson,
      });
    },
    handleConfirmDrag(data) {
      this.form.imageJson = data.imageJson;
      this.form.schematicImage = data.ossId;
    },
    // 提交按钮
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (!valid) return;
        let form = { ...this.form };
        form.basicInfo = JSON.stringify(form.basicInfo);
        if (this.status == "1") {
          entrustEquipmentAdd(form).then((response) => {
            this.$modal.msgSuccess("新增成功");
            this.dialogVisible = false;
            this.getList();
          });
        } else if (this.status == "2") {
          entrustEquipmentEdit(form).then((response) => {
            this.$modal.msgSuccess("修改成功");
            this.dialogVisible = false;
            this.getList();
          });
        }
      });
    },
    // 删除
    handleDelete(row) {
      this.$modal
        .confirm(`是否确认删除设备[${row.equipmentName}]？`)
        .then(() => {
          this.loading = true;
          return entrustEquipmentDelete(row.equipmentId);
        })
        .then(() => {
          this.loading = false;
          this.$modal.msgSuccess("删除成功");
          this.getList();
        })
        .finally(() => {
          this.loading = false;
        });
    },
    // 导出
    handleExport() {
      this.download(
        "/basic/entrust/equipment/export",
        {
          ...this.queryParams,
        },
        `设备列表.xlsx`
      );
    },
    // 上传点位图
    handleUploadPointImg() {
      this.showUploadPointImageDialog = true;
    },
    // 表单重置
    reset() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
      };
      this.resetForm("queryParams");
      this.getList();
    },
  },
};
</script>

<style lang="scss" scoped>
.table {
  margin-bottom: 20px;
  padding-inline: 20px;
}
.mask {
  position: relative;
  &::after {
    content: "";
    position: absolute;
    top: 0;
    bottom: 0;
    right: 0;
    z-index: 999;
    cursor: not-allowed;
    left: 0;
    background: #fff0;
  }
}
.innertitle {
  margin-bottom: 20px;
  position: relative;
  color: #252d57;
  font-weight: bold;
  padding-left: 15px;
  font-size: 18px;
  line-height: 20px;
  &::before {
    content: "";
    width: 5px;
    height: 100%;
    background-color: #2a72ee;
    border-radius: 5px;
    position: absolute;
    left: 0;
    bottom: 0;
  }
}
.line ::v-deep .el-form-item__label {
  line-height: 18px;
  text-align: justify;
  text-align-last: justify;
}
.line-height ::v-deep .el-form-item__label {
  height: 100px;
}
.equipmentform ::v-deep .el-form-item__label {
  margin-right: 20px;
}
.split {
  height: 10px;
}
::v-deep .inline .el-form-item__label {
  line-height: 16px;
}
</style>
